const WXAPI = require('apifm-wxapi')
const ShopUtil = require('../../utils/shop')
const wxaCode = require("../../utils/auth");
const share = require("../../utils/share")
const AUTH = require("../../utils/auth")
Page({
    data: {
        id: 0,
        page:1,
        pageSize : 10,
        detail:{},
        currentUser:{}
    },
    onLoad: async function (options) {
      await AUTH.checkHasLogined()
        const id = options.id;
        this.setData({
            id
        })
        this.loadGroup()
        this.getUserInfo()
        
    },
    phoneCall(){
        wx.makePhoneCall({
            phoneNumber:  this.data.detail.shop.linkPhone
        })
    },
    openAddress(){
        wx.openLocation( this.data.detail.shop)
    },
    openGroupAddress(){
        wx.openLocation( this.data.detail.address)
    },
    async join(){
        const shop = this.data.detail.shop
         wx.setStorageSync('shopIds',shop.id)
         wx.setStorageSync("groupId",this.data.detail.base.id)
        await  ShopUtil.getShop(this.data.detail.base.shopId)
        wx.switchTab({url:"/pages/index/index", success:function () {
                var page = getCurrentPages().pop();
                if (page == undefined || page == null) return;
                page.onLoad({});   //重新刷新页面
            }})
    },
   async  loadGroup() {
       
      const {id,page,pageSize } = this.data
       const reqData = {
           id,page,pageSize,
       }
      const res = await  WXAPI.groupDetail(reqData)
       this.setData({
           detail :res.data
       })
      
    },
    onShareAppMessage() {
        const group = this.data.detail
       const data = {...group.base,shop: group.shop}
       return  share.inviteToGroup(data)

    },
    async getUserInfo(){
      const res = await WXAPI.userDetail(wx.getStorageSync('token'))
      if(res.code ===0){
        this.setData({
          currentUser: res.data.base
        })
      }
      console.log(this.data.currentUser);
    }
});